package com.ewanghuiju.app.ui.watermail.activity;

import android.view.View;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.ewanghuiju.app.R;
import com.ewanghuiju.app.app.Constants;
import com.ewanghuiju.app.base.RootActivity;
import com.ewanghuiju.app.base.contract.watermail.WaterOrderListContract;
import com.ewanghuiju.app.model.bean.response.WaterOrderListResponBean;
import com.ewanghuiju.app.presenter.watermail.WaterOrderListPresenter;
import com.ewanghuiju.app.ui.watermail.adapter.WaterOrderListAdapter;
import com.ewanghuiju.app.util.LoadingDialogUtils;
import com.ewanghuiju.app.util.StartActivityUtil;
import com.gyf.immersionbar.ImmersionBar;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadMoreListener;

import java.util.ArrayList;
import java.util.List;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import butterknife.BindView;

/**
 * @author sj  qq:751943509
 * @class 水滴/积分订单列表
 * @time 2020/2/19
 */
public class WaterOrderListActivity extends RootActivity<WaterOrderListPresenter> implements WaterOrderListContract.View {

    @BindView(R.id.view_main)
    RecyclerView recyclerView;
    @BindView(R.id.refreshLayout)
    SmartRefreshLayout refreshLayout;

    private WaterOrderListAdapter adapter;

    private int currentPage = 1;

    @Override
    protected int getLayout() {
        return R.layout.activity_water_orderlist;
    }

    @Override
    protected void initEventAndData() {
        super.initEventAndData();
        initRefreshLayout();
        initRecylerView();
    }

    @Override
    protected int getTitleIntText() {
        return R.string.water_record;
    }

    private void initRecylerView() {
        recyclerView.setNestedScrollingEnabled(false);
        LinearLayoutManager mLinearLayoutManager = new LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false);
        recyclerView.setLayoutManager(mLinearLayoutManager);
        adapter = new WaterOrderListAdapter(R.layout.item_water_order_list);
        adapter.setHasStableIds(true);
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                WaterOrderListResponBean bean = (WaterOrderListResponBean) adapter.getItem(position);
                new StartActivityUtil(mContext, WaterOrderDetailsActivity.class).putExtra(Constants.WATER_SHOP_ID, bean.getId() + "")
                        .startActivity(true);
            }
        });
        recyclerView.setAdapter(adapter);
        LoadingDialogUtils.show(mContext);
        currentPage = 1;
        mPresenter.getData(currentPage);
    }

    private void initRefreshLayout() {
        refreshLayout.setEnableLoadMore(true);
        refreshLayout.setEnableLoadMore(true);
        refreshLayout.setOnRefreshLoadMoreListener(new OnRefreshLoadMoreListener() {
            @Override
            public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
                LoadingDialogUtils.show(mContext);
                currentPage++;
                mPresenter.getData(currentPage);
            }

            @Override
            public void onRefresh(@NonNull RefreshLayout refreshLayout) {
                LoadingDialogUtils.show(mContext);
                currentPage = 1;
                refreshLayout.resetNoMoreData();
                mPresenter.getData(currentPage);
            }
        });
    }

    @Override
    protected void intSmartRefreshLayout() {
//        super.intSmartRefreshLayout();
    }

    @Override
    protected void initImmersionBar() {
        super.initImmersionBar();
        ImmersionBar.with(this)
                .titleBar(mToolbar, true)
                .statusBarDarkFont(true)
                .init();
    }

    @Override
    protected void initInject() {
        getActivityComponent().inject(this);
    }

    @Override
    public void showContent(List<WaterOrderListResponBean> list) {
        LoadingDialogUtils.dismissDialog_ios();
        if (list == null) {
            list = new ArrayList<>();
        }
        if (currentPage == 1) {
            if (list.size() < Constants.NUM_OF_PAGE) {
                refreshLayout.finishRefreshWithNoMoreData();
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishRefresh();
            }
            adapter.setNewData(list);
        } else {
            if (list.size() < Constants.NUM_OF_PAGE) {
                refreshLayout.finishLoadMoreWithNoMoreData();
            } else {
                refreshLayout.finishLoadMore();
            }
            adapter.addData(list);
        }

        if (adapter.getData().size() > 0) {
            stateMain();
        } else {
            stateEmpty();
        }
    }

    @Override
    public void showContentError() {
        LoadingDialogUtils.dismissDialog_ios();
        if (currentPage == 1) {
            refreshLayout.finishRefresh();
        } else {
            currentPage--;
            refreshLayout.finishLoadMore();
        }
        if (adapter.getData().size() == 0) {
            super.stateError();
        }
    }
}
